<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<style>
	body,html{
		margin: 0 auto;
		height:100%;
		width:100%;
	}
</style>
</head>
<body>
	<div id="sys_departmentMain" style="width:100%;height:100%;overflow: auto;" class="divfrist">
			<DIV id="" style="width:100%;height:0.7%;overflow:hidden;"></DIV>
			<DIV id="" style="width:100%;height:98.6%;">
					<DIV id="" class="" style="float:left;width:0.4%;height:100%;overflow:hidden;"></DIV>
					<DIV id="" class="" style="float:left;width:20%;height:100%;">
							<DIV id="" class="allborder" style="width:100%;height:100%;max-height:100%;max-width:100%;overflow:auto;">
							   <!--内容区 -->
								 <ul id="addDepartment_tree" class="ztree"></ul>	
							   <!--内容区 -->
							</DIV>
					</DIV>
					<DIV id="" class="" style="float:left;width:0.4%;height:100%;overflow:hidden;"></DIV>
					<DIV id="" class="" style="float:left;width:78.8%;height:100%;">
							<DIV id="" class="allborder" style="width:100%;height:100%;max-height:100%;max-width:100%;">
								<!-- 内容区 -->
											<div style="width:100%;height:100%;">		
													 <table  id="DepartmentTable" border="0"></table>
											</div>
											<div id="Department_tb" style="overflow: hidden; padding-left: 10px; padding-top: 5px;padding-bottom: 5px;">
												 <a href="javascript:void(0);" class="easyui-linkbutton"   data-options="iconCls:'icon-add',plain:true" onclick="window.Department.addDepartment()">增加部门</a>||	
												 <a href="javascript:void(0);" class="easyui-linkbutton"   data-options="iconCls:'icon-edit',plain:true" onclick="window.Department.updateDepartment()">编辑部门</a>||
												 <a href="javascript:void(0);" class="easyui-linkbutton"   data-options="iconCls:'icon-no',plain:true" onclick="window.Department.deleteDepartment()">删除部门</a>||	    	
											</div>		
								<!-- 内容区 -->
							</DIV>
					</DIV>
					<DIV id="" class="" style="float:left;width:0.37%;height:100%;overflow:hidden;"></DIV>
			</DIV>
			<DIV id="" class="div_bottom"></DIV>
</div>
<script type="text/javascript">
(function(){
	//增加部门
	function addDepartment(){
		var treeObj = $.fn.zTree.getZTreeObj("addDepartment_tree");
		var nodes = treeObj.getSelectedNodes();
		if(nodes.length<=0){
			alert("未选中一个部门,不能编辑");
			return;
		}
		
		if($("#Department_addDepartment").length>0){
			$("#Department_addDepartment").window("open");
		}
		$('<div id="Department_addDepartment"></div>').window({
		    title: '增加部门', 		
		    iconCls:'icon-edit', 
		    width:650,   
		    height:400,   
		    resizable:true,
		    closed: true,   
		   	broder:false,
		    draggable:false,
		    modal: true,
		    content:$('<div style="width:100%;height:100%;"></div>').load("${pageContext.request.contextPath}/html/system/departmentAdd.jsp"),
		    onClose:function(){
				$("#Department_addDepartment").window("destroy",true);
				 createTree();
		    }
		});
		$("#Department_addDepartment").window("open");
		
	}
	//更新部门
	function updateDepartment(){
		var treeObj = $.fn.zTree.getZTreeObj("addDepartment_tree");
		var nodes = treeObj.getSelectedNodes();
		
		if(nodes.length<=0){
			alert("未选中一个部门,做为新增部门的上级部门");
			return;
		}
		if(nodes[0].id==1){
			alert("不能更新根节点 ");
			return;
		}
		if($("#Department_updateDepartment").length>0){
			$("#Department_updateDepartment").window("open");
		}
		$('<div id="Department_updateDepartment"></div>').window({
		    title: '编辑部门', 		
		    iconCls:'icon-add', 
		    width:650,   
		    height:400,   
		    resizable:true,
		    closed: true,   
		    draggable:false,
		    modal: true,
		    content:$('<div style="width:100%;height:100%;"></div>').load("${pageContext.request.contextPath}/html/system/departmentAdd.jsp?departmentId="+nodes[0].uuid),
		    onClose:function(){
				$("#Department_updateDepartment").window("destroy",true);
				 createTree();
		    }
		});
		$("#Department_updateDepartment").window("open");
		
	}
	//树的点击事件
	function onClick(event, treeId, treeNode){
		$.ajax({
			   type: "POST",
			   url: "${pageContext.request.contextPath}/sys/sysDepartmentAction!getChildrenDepartMent.action",
			   data:{departMentTID:treeNode.id},
			   dataType:'json',
			   success: function(msg){
				   $('#DepartmentTable').datagrid("loadData",msg);
			   }
		});	
		
	}
	//创建树
	function createTree(treeDate){
		var setting = {
				data: {
					simpleData: {
						enable: true
					}
				},
				callback: {
					onClick: onClick
				}
		};
		if(treeDate==null||treeDate.length==0){
			$.ajax({
				   type: "POST",
				   url: "${pageContext.request.contextPath}/sys/sysDepartmentAction!getDepartMentTree.action",
				   data:{roleId:0},
				   dataType:'json',
				   success: function(msg){
					   var node = new Object();
					   node.uuid=0;
					   node.id=1;
					   node.pid=0;
					   node.name="根部门";
					   node.open=true;
					   msg.push(node);
					   $.fn.zTree.init($("#addDepartment_tree"), setting, msg);     
				   }
			});	
		}else{
			 $.fn.zTree.init($("#addDepartment_tree"), setting, treeDate);    
		}
	}
	//创建部门table
	function createDepartmentTable(){
			$('#DepartmentTable').datagrid({
				fit: true,
				toolbar:"#Department_tb",
				singleSelect:false,
				rownumbers: true,
				animate:true,
				striped: true,
				loadMsg:'正在加载数据',
				collapsible:true,
				idField:'departmentId',
				fitColumns:true,
				columns:[[
			  		{field:'departMentName',title:'部门名称',width:120},
			  		{field:'departMent_dituy',title:'部门责任人',width:120},
			  		{field:'departMentNote',title:'部门描述',width:120},
			  		{field:'departMentCreateDate1',title:'创建时间',width:120}
			  		]]
					//pagination:true
				});
	}
	//删除一个部门和所有子部门
	function deleteDepartment(){
		var treeObj = $.fn.zTree.getZTreeObj("addDepartment_tree");
		var nodes = treeObj.getSelectedNodes();
		if(nodes.length<=0){
			alert("未在左侧树节点中选择一个部门");
			return;
		}
		$.messager.confirm('删除部门', '删除部门，会删除部门下的所有子部门,你确定你需要删除这些部门?', function(r){
			if (r){
				$.ajax({
					   type: "POST",
					   url: "${pageContext.request.contextPath}/sys/sysDepartmentAction!deleteDepartment.action",
					   data:{departMentTID:nodes[0].id},
					   dataType:'text',
					   success: function(msg){
						   if(msg=="success"){
							   alert("删除成功");
							   createTree();
							   $('#DepartmentTable').datagrid("loadData",[]);
						   }else{
							   alert("删除失败")
						   }
					   }
				});	
			}
		});
	}
	//注册所有事件
	window["Department"]={};
	window["Department"]["addDepartment"]=addDepartment;
	window["Department"]["updateDepartment"]=updateDepartment;
	window["Department"]["deleteDepartment"]=deleteDepartment;
	$(function(){	
		$.parser.parse('#sys_departmentMain'); 
		createDepartmentTable();
		createTree(null);
	});
})();
</script>
</body>
</html>